package com.easy.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述:
 *
 * @author gwj
 * @date 2024年11月25日 14:24
 */

public class PartitionJobStatus {

    private static final Map<String, String> JOB_STATUS = new ConcurrentHashMap<>();

    public static final String STOP="STOP";

    public static final String RUN="RUN";

    public static final String RUN_END_STOP="RUN_END_STOP";


    /**
     * 描述 : 记录任务池允许状态
     * @author gwj
     * @date 2024/11/25 14:36
     * @param partition 分区标识
     */
    public static void recordJobStatus(String partition){
        JOB_STATUS.put(partition,RUN);
    }

    /**
     * 描述 : 修改任务池允许状态
     * @author gwj
     * @date 2024/11/25 14:36
     * @param partition 分区标识
     * @return boolean
     */
    public synchronized static boolean stopImmediately(String partition){
        String status = JOB_STATUS.get(partition);
        if(status != null){
            JOB_STATUS.put(partition,STOP);
            return true;
        }
        return false;
    }

    /**
     * 描述 : 任务池清空后关闭任务池
     * @author gwj
     * @date 2024/11/25 14:55
     * @param partition 分区key
     * @return boolean
     */
    public synchronized static boolean taskFinishedStop(String partition){
        String status = JOB_STATUS.get(partition);
        if(status != null){
            JOB_STATUS.put(partition,RUN_END_STOP);
            return true;
        }
        return false;
    }

    /**
     * 描述 : 获取当前任务池状态
     * @author gwj
     * @date 2024/11/25 14:36
     * @param partition 分区标识
     * @return boolean
     */
    public static boolean getStatus(String partition){
        return RUN.equals(JOB_STATUS.get(partition));
    }
    public static String  getStatusFlag(String partition){
        return JOB_STATUS.get(partition);
    }


}
